package com.algorithms.sorts;

/**
 * BubbleSort<br>
 * Complexity O(N^2)<br><br>
 * 
 * we run the 2 loops and compare 2 items and swap if the greater is at
 * the left. by the end of each loop, the biggest item will be found and
 * placed to the most right.
 * 
 * @author minhld
 *
 */
public class BubbleSort extends Sort{
	
	public BubbleSort(int[] dataArray, int dataLength){
		super(dataArray, dataLength);
		this.sortName = "BubbleSort";
	}
	
	@Override
	public int[] sort(){
		for (int i = this.length - 1; i >= 1; i--){
			for (int j = 1; j <= i; j++){
				if (this.data[j - 1] > this.data[j]){
					swap(j, j - 1);
					
					this.numOfCopy += 2;
				}
				
				this.numOfProcess++;
			}
		}
		
		return this.data;
	}
}
